package com.tl.sboot.service.impl;

import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mysql.fabric.xmlrpc.base.Data;
import com.tl.sboot.mapper.RedisMapper;
import com.tl.sboot.service.RedisService;
import com.tl.sboot.utils.CSVUtils;
import com.tl.sboot.utils.DateUtil;
import com.tl.sboot.utils.RedisUtil;
import com.tl.sboot.utils.StkSqlUtil;
/**
 * 2016年12月9日17:15:44 陈君文
 * redisService
 * @author Administrator
 *
 */
@Service
public class RedisServiceImpl implements RedisService{
	@Autowired
	RedisMapper redisMapper;
	@Autowired
	RedisUtil ru;
	@Override
	public Map<String, Set<String>> findSTK_COM_PROFILE() {
		List<Map<String, Object>> scpListMap = redisMapper.findSTK_COM_PROFILE();
		Map<String, Set<String>> provinceMap =new HashMap<>();
		for (Map<String, Object> map : scpListMap) {
				String CITY = (String) map.get("CITY");
				String GICS_INDU_CODE = (String) map.get("GICS_INDU_CODE");
				String GICS_INDU_NAME = (String) map.get("GICS_INDU_NAME");
				String PROVINCE = (String) map.get("PROVINCE");//省份
				String STOCKCODE =  (String) map.get("STOCKCODE");//股票代码
				Set<String> set = null;
				int i=10000;
				if (provinceMap.containsKey(PROVINCE)) {
					set = provinceMap.get(PROVINCE);
				}else{
					 set = new HashSet<>();
				}
				if (!set.contains(CITY)) {
					i++;
					set.add(CITY);
					ru.hset("CITY", CITY,""+i);
				};
				provinceMap.put(PROVINCE, set);
				ru.sadd("SE.CITY."+CITY, STOCKCODE);
				ru.sadd("SE.GICS_INDU."+GICS_INDU_CODE, STOCKCODE);
				ru.hset("GICS_INDU_NAME", GICS_INDU_CODE, GICS_INDU_NAME);
				
		}
		return provinceMap;
	}
	@Override
	public Set<String> interSTOCKCODE(String city, String GICS_INDU) {
		Set<String> region = ru.smembers("SE.CITY."+city);
		Set<String> gics_indu = ru.smembers("SE.GICS_INDU."+GICS_INDU);
		System.out.println(region);
		System.out.println(gics_indu);
		Set<String> sinter = ru.sinter("SE.CITY."+city,"SE.GICS_INDU."+GICS_INDU);
		return sinter;
	}
	@Override
	public Map<String, Object> findSTK_CASH_GENC_Count() {
		String sql = "select ";
		List<Map<String,String>> fields = redisMapper.findSTK_CASH_GENField();
		for (int i = 0; i < fields.size(); i++) {
			Map<String, String> map = fields.get(i);
			String field = map.get("COLUMN_NAME");
			if (fields.size()-1!=i) {
				sql += "count("+field+"),";
			}else {
				sql += "count("+field+")";
			}
		}
		sql += " FROM STK_CASH_GEN where RPT_SRC='年报' and RPT_TYPE='合并' and ENDDATE='2015-12-31 00:00:00'";
		Map<String, Object> fieldCount = redisMapper.findSTK_CASH_GENC_Count(sql);
		
		String fieldsStr = "";
		List<String> csv = new ArrayList<>();
		csv.add("列名,个数");
		Set<String> keySet = fieldCount.keySet();
		for (String key : keySet) {
			Long long2 = (Long) fieldCount.get(key);
			if(long2>=10000){
				csv.add(key+","+long2);
				String key1 = key.substring(6, key.indexOf(")"));
				fieldsStr += key1+",";
			}
		}
		boolean isSuccess=CSVUtils.exportCsv(new File("D:/巨灵数据统计/STK_CASH_GEN-现金流量表.csv"), csv);
		if (isSuccess) {
			System.out.println("现金流量表.csv生成文件成功");
		}
		String redisSql = "select "+fieldsStr+"STOCKCODE FROM STK_CASH_GEN scg INNER JOIN STK_CODE sc ON scg.COMCODE = sc.COMCODE where RPT_SRC='年报' and RPT_TYPE='合并' and ENDDATE='2015-12-31 00:00:00' and sc.ISVALID =1 AND stocksname IS NOT NULL AND STK_TYPE = 'A股'";
		System.out.println(redisSql);
		List<String> csv1 = new ArrayList<>();
		csv1.add("列名,对应股票代码");
		List<Map<String, Object>> scgList = redisMapper.findSTK_CASH_GEN(redisSql);
		for (Map<String, Object> map : scgList) {
			String STOCKCODE = (String) map.get("STOCKCODE");
			map.remove("STOCKCODE");
			Set<String> keys = map.keySet();
			for (String key : keys) {
				String value = map.get(key).toString();
				String ckey = StkSqlUtil.getFieldName(key);
				double d = Double.parseDouble(value);
				ru.zadd("SS.现金流量表."+ckey, d, STOCKCODE);
				//csv1.add(key+","+STOCKCODE);
			}
			
			
		}
		/*boolean isSuccess1=CSVUtils.exportCsv(new File("D:/巨灵数据统计/现金流量表_字段-股票代码为空.csv"), csv1);
		if (isSuccess1) {
			System.out.println("现金流量表_字段-股票代码为空.csv生成文件成功");
		}*/
		return fieldCount;
	}

	@Override
	public Map<String, Object> findSTK_INCOME_GENP_Count() {
		String sql = "select ";
		List<Map<String,String>> fields = redisMapper.findSTK_INCOME_GENField();
		for (int i = 0; i < fields.size(); i++) {   
			Map<String, String> map = fields.get(i);
			String field = map.get("COLUMN_NAME");
			if (fields.size()-1!=i) {
				sql += "count("+field+"),";
			}else {
				sql += "count("+field+")";
			}
		}
		sql += " FROM STK_INCOME_GEN where RPT_SRC='年报' and RPT_TYPE='合并' and ENDDATE='2015-12-31 00:00:00'";
		Map<String, Object> fieldCount = redisMapper.findSTK_INCOME_GENP_Count(sql);
		List<String> csv = new ArrayList<>();
		csv.add("列名,个数");
		Set<String> keySet = fieldCount.keySet();
		String fieldsStr = "";
		for (String key : keySet) {
			Long long2 = (Long) fieldCount.get(key);
			if(long2>=10000){
				csv.add(key+","+long2);
				String key1 = key.substring(6, key.indexOf(")"));
				fieldsStr += key1+",";
			}
		}
		boolean isSuccess=CSVUtils.exportCsv(new File("D:/巨灵数据统计/STK_INCOME_GEN-利润表（通用）.csv"), csv);
		if (isSuccess) {
			System.out.println("利润表.csv生成文件成功");
		}
		String redisSql = "select "+fieldsStr+"STOCKCODE FROM STK_INCOME_GEN scg INNER JOIN STK_CODE sc ON scg.COMCODE = sc.COMCODE where RPT_SRC='年报' and RPT_TYPE='合并' and ENDDATE='2015-12-31 00:00:00' and sc.ISVALID =1 AND stocksname IS NOT NULL AND STK_TYPE = 'A股'";
		System.out.println(redisSql);
		List<String> csv1 = new ArrayList<>();
		csv1.add("列名,对应股票代码");
		List<Map<String, Object>> scgList = redisMapper.findSTK_INCOME_GEN(redisSql);
		for (Map<String, Object> map : scgList) {
			String STOCKCODE = (String) map.get("STOCKCODE");
			map.remove("STOCKCODE");
			Set<String> keys = map.keySet();
			for (String key : keys) {
				String value = map.get(key).toString();
				double d = Double.parseDouble(value);
				String ckey = StkSqlUtil.getFieldName(key);
				ru.zadd("SS.利润表."+ckey, d, STOCKCODE);
				//csv1.add(key+","+STOCKCODE);
				
			}
		}
		/*boolean isSuccess1=CSVUtils.exportCsv(new File("D:/巨灵数据统计/利润表（通用）_字段-股票代码为空.csv"), csv1);
		if (isSuccess1) {
			System.out.println("利润表（通用）_字段-股票代码为空.csv生成文件成功");
		}*/
		return fieldCount;
	}

	@Override
	public Map<String, Object> findSTK_BALA_GENB_Count() {
		String sql = "select ";
		List<Map<String,String>> fields = redisMapper.findSTK_BALA_GENField();
		for (int i = 0; i < fields.size(); i++) {
			Map<String, String> map = fields.get(i);
			String field = map.get("COLUMN_NAME");
			if (fields.size()-1!=i) {
				sql += "count("+field+"),";
			}else {
				sql += "count("+field+")";
			}
		}
		sql += " FROM STK_BALA_GEN where RPT_SRC='年报' and RPT_TYPE='合并' and ENDDATE='2015-12-31 00:00:00'";
		Map<String, Object> fieldCount = redisMapper.findSTK_BALA_GENB_Count(sql);
		List<String> csv = new ArrayList<>();
		csv.add("列名,个数");
		Set<String> keySet = fieldCount.keySet();
		String fieldsStr = "";
		for (String key : keySet) {
			Long long2 = (Long) fieldCount.get(key);
			if(long2>=10000){
				csv.add(key+","+long2);
				String key1 = key.substring(6, key.indexOf(")"));
				fieldsStr += key1+",";
			}
		}
		boolean isSuccess=CSVUtils.exportCsv(new File("D:/巨灵数据统计/STK_BALA_GEN-资产负债表.csv"), csv);
		if (isSuccess) {
			System.out.println("资产负债表.csv生成文件成功");
		}
		String redisSql = "select "+fieldsStr+"STOCKCODE FROM STK_BALA_GEN scg INNER JOIN STK_CODE sc ON scg.COMCODE = sc.COMCODE where RPT_SRC='年报' and RPT_TYPE='合并' and ENDDATE='2015-12-31 00:00:00' and sc.ISVALID =1 AND stocksname IS NOT NULL AND STK_TYPE = 'A股'";
		System.out.println(redisSql);
		List<String> csv1 = new ArrayList<>();
		csv1.add("列名,对应股票代码");
		List<Map<String, Object>> scgList = redisMapper.findSTK_INCOME_GEN(redisSql);
		for (Map<String, Object> map : scgList) {
			String STOCKCODE = (String) map.get("STOCKCODE");
			map.remove("STOCKCODE");
			Set<String> keys = map.keySet();
			for (String key : keys) {
				String value = map.get(key).toString();
				String ckey = StkSqlUtil.getFieldName(key);
				double d = Double.parseDouble(value);
				ru.zadd("SS.资产负债表."+ckey, d, STOCKCODE);
				//csv1.add(key+","+STOCKCODE);
				
			}
			
			
		}
		/*boolean isSuccess1=CSVUtils.exportCsv(new File("D:/巨灵数据统计/资产负债表_字段-股票代码为空.csv"), csv1);
		if (isSuccess1) {
			System.out.println("资产负债表_字段-股票代码为空.csv生成文件成功");
		}*/
		return fieldCount;
	}
	@Override
	public List<Map<String, String>> findGicsByCity(String city) {
		return redisMapper.findGicsByCity(city);
	}
	@Override
	public void addCXGToRedis() {
		List<Map<String, String>> findCXG = redisMapper.findCXG();
		for (Map<String, String> map : findCXG) {
			String string = map.get("STOCKCODE");
			ru.sadd("SS.CXG", string);
		}
		ru.expire("SS.CXG", DateUtil.getNextDaySec());
	}
}
